<?php
//引入系统配置文件
include_once('config/init.php');
include_once('check.php');

// 判断是否有post过来的数据
$action = isset($_POST['action']) ? trim($_POST['action']) : '';

// 如果接收到的数据为delete,说明要删除
if($action == "delete"){
    // 封装一个返回结果
    $success = [
        "result" => false, //是否成功
        "msg" => '' //返回信息
    ];

    // 获取到删除数据的id
    $ids = isset($_POST['ids']) ? trim($_POST['ids']) : 0;
    // var_dump($ids);
    // exit;

    // 先查询出要删除的数据
    $sql = "SELECT * FROM {$pre_}job WHERE id IN ($ids)";
    $dellist = all($sql);
    // var_dump($dellist);
    // exit;

    // 如果没有查询到
    if(empty($dellist)){
        $success['result'] = false;
        $success['msg'] = "暂无删除的数据";
        
        // 将php的数据转换为json类型,并返回ajax
        echo json_encode($success);
        exit;
    }

    $where = "id IN($ids)";
    $affect = delete("job",$where);

    if($affect){
        // 删除成功
        $success['result'] = true;
        $success['msg'] = "删除成功";
        
    }else{
        $success['result'] = false;
        $success['msg'] = "删除失败";
    }
    echo json_encode($success);
    exit;
}

//当前页码
$page = isset($_GET['page']) ? trim($_GET['page']) : 1;

//每页显示多少条
$limit = 5;

//中间显示多少个页码数
$size = 5;

//sql查询数据总数
$sql = "SELECT COUNT(id) AS c FROM {$pre_}job";
$count = find($sql);
$count = isset($count['c']) ? trim($count['c']) : 0;

//调用分页函数
$html = page($page, $count, $limit, $size, 'black2');

//偏移量
$start = ($page-1)*$limit;

//链表查询

// 职位表.depid = 部门表.id
$sql = "SELECT job.*,dep.name AS depname FROM {$pre_}job AS job LEFT JOIN {$pre_}department AS dep ON job.depid = dep.id ORDER BY job.id  LIMIT $start,$limit";

//调用函数
$list = all($sql);

// var_dump($list);
// exit;

?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <!-- 引入公共样式 -->
        <?php include_once('meta.php');?>

        <!-- 分页样式 -->
        <link rel="stylesheet" href="assets/css/page.css" />
    </head>

    <body>
        <!-- 引入头部 -->
        <?php include_once('header.php');?>

        <!-- 引入菜单 -->
        <?php include_once('menu.php');?>

        <div class="content">
            <div class="header">
                <h1 class="page-title">职位列表</h1>
            </div>
            <ul class="breadcrumb">
                <li><a href="index.php">Home</a> <span class="divider">/</span></li>
                <li class="active">职位列表</li>
            </ul>

            <div class="container-fluid">
                <div class="row-fluid">
                    <div class="btn-toolbar">
                        <button class="btn btn-primary" onClick="location='AddJob.php'"><i class="icon-plus"></i>添加职位</button>
                    </div>
                    <div class="well">
                        <table class="table">
                            <thead>
                                <tr>
                                    <th><input type="checkbox" id="toggle"/></th>
                                    <th>ID</th>
                                    <th>职位名称</th>
                                    <th>所在部门</th>
                                    <th style="width: 60px;">操作</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach($list as $item){?>
                                <tr>
                                    <td><input type="checkbox" name="list" value="<?php echo $item['id'] ?>"/></td>
                                    <td><?php echo $item['id'];?></td>
                                    <td><?php echo $item['name'];?></td>
                                    <td><?php echo $item['depname'];?></td>
                                    <td>
                                        <a href="EditJob.php?id=<?php echo $item['id'] ?>"><i class="icon-pencil"></i></a>
                                        <a class="delone" data-ids="<?php echo $item["id"]; ?>" href="#myModal" role="button" data-toggle="modal"><i class="icon-remove"></i></a>
                                    </td>
                                </tr>
                                <?php }?>
                                <tr>
                                    <td>
                                        <!-- 点击是一个锚点 -->
                                        <a class="btn btn-success delall" href="#myModal" role="button" data-toggle="modal">
                                            <i class="icon-remove"></i>批量删除
                                        </a>
                                    </td>
                                </tr>
                            </tbody>
                        </table>
                    </div>
                    <?php echo $html;?>

                    <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">删除提示框</h3>
                        </div>
                        <div class="modal-body">
                            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>是否确定删除?</p>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                            <button class="btn btn-danger confirm" data-dismiss="modal">确认删除</button>
                        </div>
                    </div>

                    <footer>
                        <hr>
                        <p>&copy; 2017 <a href="#" target="_blank">copyright</a></p>
                    </footer> 
                </div>
            </div>
        </div>
    </body>
</html>
<script>
    // 全局变量
    var ids = [];

    // 给选框绑定全选反选
    $("#toggle").click(function(){
        // 获取选中状态
        // console.log($(this).prop("checked"));
        $("input[name=list]").prop("checked",$(this).prop("checked"));
    })

    // 点击了批量删除
    $(".delall").click(function(){
        // 获取到列表中所有被选中的元素
        var list = $("input[name=list]:checked");
        // console.log(list);
        if(list.length <= 0){
            alert("未选中要删除的元素");
            return false;
        }
        
        var arr = [];
        
        // 循环元素
        list.each(function(index,item){
            arr.push(item.value);
        })
        // console.log(arr);

        // 覆盖全局变量
        ids = arr;

        // console.log(ids);
    })

    // 单个删除
    $(".delone").click(function(){
        // 获取点击元素身上的ids属性
        var id = $(this).data('ids');
        // 把获取到的值放到全局变量中
        ids = [id]
        // console.log(ids);
    })

    // 点击确认删除
    $(".confirm").click(function(){
        // 手动关闭模态框
        $("#myModal").modal('hide');

        // 将数组转换为字符串
        var str = ids.join(',');
        // console.log(str);

        // 发送ajax请求
        $.ajax({
            url:'JobList.php',
            type:'post', //请求类型
            dataType:'json', //返回数据类型
            data:{action:'delete',ids:str},
            success:function(success){
                if(success.result){
                    location.href = "JobList.php";
                }else{
                    alert(success.msg);
                }
                return false;
            },
            error:function(error){
                console.log(error);
            }
        })
    })

   
</script>